﻿using Microsoft.Practices.EnterpriseLibrary.Validation.Configuration;
using Microsoft.Practices.Unity;


namespace ElValWeb.ValidatorClientAdapters {
    public class ClientAdapterFactory {
        
        private static IUnityContainer _container = new UnityContainer();

        static ClientAdapterFactory() {
            _container.RegisterType<BaseClientAdapter, NotNullValidatorClientAdapter>(typeof (NotNullValidatorData).Name);
            _container.RegisterType<BaseClientAdapter, StringLengthValidatorClientAdapter>(typeof(StringLengthValidatorData).Name);
            _container.RegisterType<BaseClientAdapter, OrCompositeValidatorClientAdapter>(typeof(OrCompositeValidatorData).Name);
            _container.RegisterType<BaseClientAdapter, AndCompositeValidatorClientAdapter>(typeof(AndCompositeValidatorData).Name);
            _container.RegisterType<BaseClientAdapter, RegularExpressionValidatorClientAdapter>(typeof(RegexValidatorData).Name);
            _container.RegisterType<BaseClientAdapter, ContainsCharactersValidatorClientAdapter>(typeof(ContainsCharactersValidatorData).Name);
            _container.RegisterType<BaseClientAdapter, DomainValidatorClientAdapter>(typeof(DomainValidatorData).Name);
            _container.RegisterType<BaseClientAdapter, RangeValidatorClientAdapter>(typeof(RangeValidatorData).Name);
        }

        public static BaseClientAdapter GetAdapter(ValidatorData validatorData) {
            string nameToCheck = validatorData.GetType().Name;
            if(_container.IsRegistered<BaseClientAdapter>(nameToCheck)) {
                return _container.Resolve<BaseClientAdapter>(nameToCheck, new ParameterOverride("validatorData", validatorData));
            }
            return new NotSupportedValidatorClientAdapter();
        }
    }
}
